﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class InsuranceCompany :  DBFObject, IDBObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }


        DateTime m_DateIssue = DateTime.MinValue;
        public DateTime DateIssue
        {
            get
            {
                return m_DateIssue;
            }
            set
            {
                m_DateIssue = value;
                OnPropertyChanged(new PropertyChangedEventArgs("DateIssue"));
            }
        }

        String m_Name = "";
        public string Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

        String m_Number = "";
        public string Number
        {
            get
            {
                return m_Number;
            }
            set
            {
                m_Number = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Number"));
            }
        }

        String m_Series = "";
        public string Series
        {
            get
            {
                return m_Series;
            }
            set
            {
                m_Series = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Series"));
            }
        }


        public InsuranceCompany()
        {
            NameTable = "INSURANCE_COMPANY_TABLE";

            DateIssue = DateTime.MinValue;
            Name = "";
            Number = "";
            Series = "";
        }

        private void CreateData()
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("DATA_ISSUE", DateIssue);
            Parameters.Add("NAME", Name);
            Parameters.Add("NUMBER_I", Number);
            Parameters.Add("SERIES", Series);

        }

        object IDBObject.Save()
        {
            try
            {
                CreateData();
                return SaveObject();
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));
            }
        }

        void IDBObject.Load(object ID)
        {
            try
            {
                CreateData();
                LoadObject(ID);

                DateIssue = DateTime.Parse( Parameters["DATA_ISSUE"].ToString());
                Name = Parameters["NAME"].ToString();
                Number = Parameters["NUMBER_I"].ToString();
                Series = Parameters["SERIES"].ToString();
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД", this.ToString()));
            }
        }


        void IDBObject.Update()
        {
            CreateData();
            UpdateObject();
        }
    }
}
